package com.cmstop.bzgd.ui.utils;

import com.cmstop.bzgd.base.BaseActivity;
import com.cmstop.bzgd.beans.FProgram;
import com.cmstop.bzgd.beans.LookBackNewsDate;
import com.cmstop.bzgd.http.AbsListNetCallback;
import com.cmstop.bzgd.http.NetUtils;
import com.cmstop.bzgd.utils.ListTypeUtils;
import com.ruihang.generalibrary.ui.fragment.BaseGeneraFragment;
import com.ruihang.generalibrary.utils.ListUtils;

import java.util.ArrayList;
import java.util.List;

public class MutilDateLookBackGetHelper {

    public static final int ERROR_NO_TODAY = 200001;
    public static final int ERROR_NO_YESTERDAY = 200002;
    public static final int ERROR_NO_GETTING = 20001;

    List<LookBackNewsDate> dates;
    private boolean shouldSetTitleAfter;
    private boolean hasAddFirtDateTitle;
    private boolean isContinue;
    private boolean isGeting;

    private LookBackNewsDate currentDate;
    private int currentIndex = -1;

    private final String ownerName;

    public MutilDateLookBackGetHelper(BaseActivity activity) {
        ownerName = activity + "";
    }

    public MutilDateLookBackGetHelper(BaseGeneraFragment fragment) {
        ownerName = fragment + "";
    }

    public int getNewsList(int size, boolean reset, AbsListNetCallback<FProgram> callback) {
        int re = -1;
        if (isGeting) {
            if (callback != null) {
                callback.onError(null, "this is in gettting", ERROR_NO_GETTING);
                callback.onEnd();
            }
            return re;
        }
        if (!ListUtils.isListValued(dates)) {
            if (callback != null) {
                callback.onError(null, "this is no dates", ERROR_NO_GETTING);
                callback.onEnd();
            }
            return re;
        }
        isGeting = true;
        if (reset) {
            reset();
        }
        List<FProgram> list = new ArrayList<>();
        if (currentDate == null || currentDate.hasGettedAll()) {
            if (nextDate()) {
                getDateNews(size, list, callback);
            } else {
                re = 0;
                if (callback != null) {
                    callback.onEnd();
                }
            }
        } else {
            re = 1;
            getDateNews(size, list, callback);
        }
        return re;
    }

    private void getDateNews(final int size, final List<FProgram> list, AbsListNetCallback<FProgram> callback) {
        NetUtils.getNetAdapter().getLookBackNewsPage(ownerName, currentDate.getProgrammeListUrl(), currentDate.getIndex() + 1
                , new AbsListNetCallback<FProgram>(ListTypeUtils.FProgram(), callback, currentDate.getIndex() + 1) {
                    private ArrayList<FProgram> lis;

                    @Override
                    public void onSuc(List<FProgram> newsItems) {
                        currentDate.setIndex(calledIndex);
                        currentDate.setTotal(getPageResponse().getData().getPageAll());
                        int to = ListUtils.getTotalSize(lis, list, calledIndex == 0 ? -2 : -1);
                        if (size > 0 && size > to) {
                            isContinue = !currentDate.hasGettedAll() || nextDate();
                            if (isContinue) {
                                if (list != null) {
                                    list.addAll(lis);
                                    getDateNews(size, list, (AbsListNetCallback) obj);
                                } else {
                                    getDateNews(size, lis, (AbsListNetCallback) obj);
                                }
                            } else {
                                if (list != null) {
                                    list.addAll(lis);
                                    ((AbsListNetCallback) obj).onSuc(list);
                                } else {
                                    ((AbsListNetCallback) obj).onSuc(lis);
                                }
                            }
                        } else {
                            if (lis != null) {
                                list.addAll(lis);
                                ((AbsListNetCallback) obj).onSuc(list);
                            } else {
                                ((AbsListNetCallback) obj).onSuc(lis);
                            }
                        }
                    }

                    @Override
                    public List<FProgram> doInBackground(List<FProgram> newsItems) {
                        lis = new ArrayList<>();
                        if (ListUtils.isListValued(newsItems)) {
//                            if (calledIndex == 0) {
////                                JLog.e("will add title and has add first " + hasAddFirtDateTitle + "\n " + MutilDateNewsGetHelper.this);
//                                Titles t = new Titles();
//                                t.setDateString(DateUtils.formatDate(DateUtils.getDataDate(currentDate), "MM月dd日"));
//                                t.setViewType(hasAddFirtDateTitle ? GreatestAdapter.VIEWTYPE_TITLE_24_CONTINUE : GreatestAdapter.VIEWTYPE_TITLE_24);
////                                if (shouldSetTitleAfter) {
////                                    newsItems.get(0).setAfterTitleMode(hasAddFirtDateTitle ? 2 : 1);
////                                }
//                                hasAddFirtDateTitle = true;
//                                lis.add(t);
//                            }
                            lis.addAll(newsItems);
                        }
                        return super.doInBackground(newsItems);
                    }

                    @Override
                    public void onError(Throwable ex, String errorMsg, int netCode) {
                        int to = ListUtils.getTotalSize(lis, list, calledIndex == 0 ? -2 : -1);
                        if (size > 0 && size > to) {
                            //根据日期请求新闻时，如果某一天的新闻获取数据失败，就继续请求下一个日期的。
                            isContinue = !currentDate.hasGettedAll() || nextDate();
                            if (isContinue) {
                                if (list != null) {
                                    getDateNews(size, list, (AbsListNetCallback) obj);
                                } else {
                                    getDateNews(size, lis, (AbsListNetCallback) obj);
                                }
                            }else if (list != null) {
                                ((AbsListNetCallback) obj).onSuc(list);
                            }
                        } else if (list != null) {
                            ((AbsListNetCallback) obj).onSuc(list);
                        }
                        ((AbsListNetCallback) obj).onError(ex, errorMsg, netCode);
                    }

                    @Override
                    public void onEnd() {
                        super.onEnd();
//                        JLog.e("in yesterday end is continue=" + isContinue);
                        if (isContinue) {
                            isContinue = false;
                        } else {
                            isGeting = false;
                            ((AbsListNetCallback) obj).onEnd();
                        }
                    }
                });
    }

    private boolean nextDate() {
        if (currentIndex < -1) {
            currentIndex = 0;
        } else if (currentIndex >= dates.size() - 1) {
            return false;
        } else {
            currentIndex++;
        }
        currentDate = dates.get(currentIndex);
        return true;
    }

    private boolean isCurrnetGetedAll() {
        return currentDate != null && currentDate.hasGettedAll();
    }

    private void reset() {
        currentDate = null;
        currentIndex = -1;
        isContinue = false;
        hasAddFirtDateTitle = false;
    }

    public void setDates(List<LookBackNewsDate> dates) {
        this.dates = dates;
    }

    public void setShouldSetTitleAfter(boolean shouldSetTitleAfter) {
        this.shouldSetTitleAfter = shouldSetTitleAfter;
    }

    public boolean hasGetAll() {
        return !ListUtils.isListValued(dates) || (currentIndex >= dates.size() - 1 && isCurrnetGetedAll());
    }

    public boolean hasValuedDate() {
        return ListUtils.isListValued(dates);
    }
}

